package com.ny.service.Impl;

import com.ny.entity.User;
import com.ny.mapper.UserMapper;
import com.ny.service.UserService;
import com.ny.until.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    // 实现用户查询逻辑（密码MD5加密后比对）
    @Override
    public User findUser(String username, String password) {
        return userMapper.findUser(username, PasswordUtil.md5(password));
    }
    //@Autowired
    //private UserMapper userMapper;

    @Override
    @Transactional
    public int deleteUser(Integer userId) {
        try {
            System.out.println("执行删除用户，ID: " + userId);
            int result = userMapper.deleteUser(userId);
            System.out.println("数据库删除结果: " + result);
            return result;
        } catch (Exception e) {
            System.err.println("删除用户服务层异常: " + e.getMessage());
            throw e;
        }
    }

    @Override
    public int addUser(User user) {
        try {
            System.out.println("添加用户: " + user.getUsername());
            return userMapper.addUser(user);
        } catch (Exception e) {
            System.err.println("添加用户异常: " + e.getMessage());
            throw e;
        }
    }


    @Override
    public User findUserByEmail(String email) {
        try {
            return userMapper.findUserByEmail(email);
        } catch (Exception e) {
            System.err.println("根据邮箱查找用户异常: " + e.getMessage());
            throw e;
        }
    }

    @Override
    public int updatePassword(String password, String email) {
        try {
            System.out.println("更新密码 - 邮箱: " + email);
            return userMapper.updatePassword(password, email);
        } catch (Exception e) {
            System.err.println("更新密码异常: " + e.getMessage());
            throw e;
        }
    }

    @Override
    public void sendToEmail(String toEmail) {
        // 邮件发送逻辑
        System.out.println("发送邮件到: " + toEmail);
        // 实际项目中应该集成邮件发送服务
    }

    @Override
    public boolean checkCode(String toEmail, String code) {
        // 验证码验证逻辑
        // 实际项目中应该从缓存或数据库中验证验证码
        return "123456".equals(code); // 模拟验证码
    }

    @Override
    public List<User> getAllUsers() {
        try {
            List<User> users = userMapper.getAllUsers();
            System.out.println("从数据库获取到 " + users.size() + " 个用户");
            return users;
        } catch (Exception e) {
            System.err.println("获取所有用户异常: " + e.getMessage());
            throw e;
        }
    }

    @Override
    public User getUserById(Integer userId) {
        try {
            User user = userMapper.getUserById(userId);
            System.out.println("根据ID获取用户 - ID: " + userId + ", 结果: " + (user != null ? user.getUsername() : "null"));
            return user;
        } catch (Exception e) {
            System.err.println("根据ID获取用户异常: " + e.getMessage());
            throw e;
        }
    }
}